import { Component, Input, ViewChild } from '@angular/core';
import { DataTableComponent, DialogService, DrawerService, TableWidthConfig } from 'ng-devui';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { SalesService } from 'src/app/@core/services/sales/sales.service';
import { SetDeliveryDateComponent } from '../set-delivery-date/set-delivery-date.component';
import { SetStartAndEndComponent } from './set-start-and-end/set-start-and-end.component';

@Component({
  selector: 'app-sales-order-to-work-order',
  templateUrl: './sales-order-to-work-order.component.html',
  styleUrls: ['./sales-order-to-work-order.component.scss']
})
export class SalesOrderToWorkOrderComponent {
  @Input() data: any;
  tableheight = '230px';
  creatTime: any = []
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  dataTableOptions = {
    columns: [
      {
        field: 'name',
        header: '单据单号',
        fieldType: 'text',
        fixedLeft: '80px'
      },
      {
        field: '物料编码',
        header: '物料编码',
        fieldType: 'text'
      },
      {
        field: '物料名称',
        header: '物料名称',
        fieldType: 'text'
      },
      {
        field: '物料规格',
        header: '物料规格',
        fieldType: 'text'
      },
      {
        field: '物料单位',
        header: '物料单位',
        fieldType: 'text'
      },
      {
        field: '数量',
        header: '数量',
        fieldType: 'text'
      },
      {
        field: '剩余数量',
        header: '剩余数量',
        fieldType: 'text'
      },
      {
        field: '生产数量',
        header: '生产数量', fieldType: 'text'
      },
      {
        field: '计划开始时间',
        header: '计划开始时间', fieldType: 'text'
      },
      {
        field: '计划结束时间',
        header: '计划结束时间', fieldType: 'text'
      },

      {
        field: '单价（含税）',
        header: '单价（含税）',
        fieldType: 'text'
      },
      {
        field: '含税价格',
        header: '含税价格',
        fieldType: 'text'
      },
      {
        field: '税额',
        header: '税额',
        fieldType: 'text'
      },
      {
        field: '折扣（%）',
        header: '折扣（%）',
        fieldType: 'text'
      },
      {
        field: '折扣单价（含税）',
        header: '折扣单价（含税）',
        fieldType: 'text'
      },
      {
        field: '折扣含税金额',
        header: '折扣含税金额',
        fieldType: 'text'
      },
      {
        field: '折扣税额',
        header: '折扣税额',
        fieldType: 'text'
      }
    ]
  };//表格数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px'
    },
    {
      field: '单据单号',
      width: '200px'
    },
    {
      field: '物料编码',
      width: '150px'
    },
    {
      field: '物料名称',
      width: '150px'
    },
    {
      field: '物料规格',
      width: '150px'
    },
    {
      field: '物料单位',
      width: '150px'
    },
    {
      field: '数量',
      width: '150px'
    },
    {
      field: '已发数量',
      width: '150px'
    },
    {
      field: '生产数量',
      width: '200px'
    },
    {
      field: '计划开始时间',
      width: '300px'
    },
    {
      field: '计划结束时间',
      width: '300px'
    },

    {
      field: '单价（含税）',
      width: '150px'
    },
    {
      field: '含税价格',
      width: '150px'
    },
    {
      field: '税额',
      width: '150px'
    },
    {
      field: '折扣（%）',
      width: '150px'
    },
    {
      field: '折扣单价（含税）',
      width: '150px'
    },
    {
      field: '折扣含税金额',
      width: '150px'
    },
    {
      field: '折扣税额',
      width: '150px'
    }
  ];//表格宽度配置
  Customer: any = []
  fifter = {
    ids: [],
    ZhuanHuanWanGongDanDe: false,
    ZhuanHuanWanDe: false,
    PartNum: '',
    PartName: '',
    CustomerName: '',
    StartTime: '',
    EndTime: '',
    SaleOrderNum: '',
    PageNumber: 1,
    PageSize: 20
  }
  @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
  tableData: any
  checkedIds: any=[]
  constructor(private SharedMethodService: SharedMethodService, private SalesService: SalesService,private DialogService:DialogService) { }
  ngOnInit(): void {
    this.fifter.ids = this.data.map((item: any) => item.id);
    this.SalesService.GetSaleOrderPartsBySaleOrderIds(this.fifter).subscribe((data: any) => {
      this.tableData = data.items;
      this.tableData.map((item: any) => {
        item.saleOrderNum = this.data.find((Item: any) => item.saleOrderId === Item.id).code
      })
      this.pager.total = data.totalCount;
    })
  }
  filter() {
    this.SalesService.GetSaleOrderPartsBySaleOrderIds(this.fifter).subscribe((data: any) => {
      this.tableData = data.items;
      this.tableData.map((item: any) => {
        item.saleOrderNum = this.data.find((Item: any) => item.saleOrderId === Item.id).code
      })
      this.pager.total = data.totalCount;
    })
  }
  reset() {
    this.fifter = {
      ids: [],
      ZhuanHuanWanGongDanDe: false,
      ZhuanHuanWanDe: false,
      PartNum: '',
      PartName: '',
      CustomerName: '',
      StartTime: '',
      EndTime: '',
      SaleOrderNum: '',
      PageNumber: 1,
      PageSize: 20
    }
    this.fifter.ids = this.data.map((item: any) => item.id);
    this.SalesService.GetSaleOrderPartsBySaleOrderIds(this.fifter).subscribe((data: any) => {
      this.tableData = data.items;
      this.tableData.map((item: any) => {
        item.saleOrderNum = this.data.find((Item: any) => item.saleOrderId === Item.id).code
      })

      this.pager.total = data.total
    })
  }
  dateChange() {
    this.fifter.StartTime = this.creatTime[0];
    this.fifter.EndTime = this.creatTime[1];
  }
  getCheckedRows() {
    this.SharedMethodService.getCheckedRowsData(this.datatable).subscribe((data: any) => {
      this.checkedIds = data;
    })

  }//获取选中行
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }) {
    this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem, this.datatable)
  }
  pageIndexChange($event: any) {

  }//切换页码
  pageSizeChange($event: any) {

  }//改变每页显示数量
  setDeliveryDate(dialogtype?: string) {
    if(this.checkedIds.length>0){
      let data = {
        display: false
      };
      const results = this.DialogService.open({
        id: 'Add',
        maxHeight: '400px',
        width: '450px',
        backdropCloseable: true,
        data: data,
        title: '设置日期',
        content: SetStartAndEndComponent,
        dialogtype: dialogtype,
        onClose: () => {
        },
        buttons: [
          {
            cssClass: 'stress',
            text: '确定',
            handler: ($event: Event) => {
              if(results.modalContentInstance.form.planStartDate&&results.modalContentInstance.form.planEndDate){
                this.tableData.map((item: any) => {
                  if (item.$checked) {
                    item.jobOrderPlanStartDate = results.modalContentInstance.form.planStartDate;
                    item.jobOrderPlanEndDate = results.modalContentInstance.form.planEndDate;
                  }
                })
                results.modalInstance.hide();
              }else{
                data.display = true;
              }
            },
          },
          {
            id: 'btn-cancel',
            cssClass: 'common',
            text: '取消',
            handler: ($event: Event) => {
              results.modalInstance.hide();
            },
          },
        ],
      })
    }else{
      const results = this.DialogService.open({
        id: 'Add',
        maxHeight: '100px',
        width: '400px',
        content:'',
        backdropCloseable: true,
        title: '请选择数据',
        dialogtype: dialogtype,
        onClose: () => {
        },
        buttons: [
          {
            cssClass: 'stress',
            text: '确定',
            handler: ($event: Event) => {
                results.modalInstance.hide();
            },
          },
          {
            id: 'btn-cancel',
            cssClass: 'common',
            text: '取消',
            handler: ($event: Event) => {
              results.modalInstance.hide();
            },
          },
        ],
      })
    }
  }
}
